package com.guet.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.guet.DTO.CoursewareDTO;
import com.guet.DTO.CoursewarePageDTO;
import com.guet.Entity.Courseware;
import com.guet.VO.CoursewareVO;
import com.guet.context.BaseContext;
import com.guet.mapper.CoursewareMapper;
import com.guet.result.PageResult;
import com.guet.service.CoursewareService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CoursewareServiceImpl implements CoursewareService {
    @Autowired
    private CoursewareMapper coursewareMapper;

    /**
     * 添加课件
     *
     * @param coursewareDTO
     */
    @Override
    public void addCourseware(CoursewareDTO coursewareDTO) {
        Long currentId = BaseContext.getCurrentId();
        Courseware courseware = new Courseware();
        BeanUtils.copyProperties(coursewareDTO, courseware);
        courseware.setUserId(currentId);
        courseware.setCreateTime(LocalDateTime.now());
        coursewareMapper.insert(courseware);
    }

    /**
     * 分页查询课件列表
     *
     * @param coursewarePageDTO
     * @return
     */
    @Override
    public PageResult list(CoursewarePageDTO coursewarePageDTO) {
        PageHelper.startPage(coursewarePageDTO.getCurrentPage(), coursewarePageDTO.getPageSize());
        Page<CoursewareVO> result = coursewareMapper.list(coursewarePageDTO);
        Long total = result.getTotal();
        List<CoursewareVO> records = result.getResult();
        return new PageResult(total, records);
    }

    /**
     * 根据id查询课件信息
     *
     * @param id
     * @return
     */
    @Override
    public CoursewareVO queryById(Integer id) {
        return coursewareMapper.queryById(id);
    }

    /**
     * 根据id删除课件资源
     *
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        coursewareMapper.deleteById(id);
    }
}
